<template>
	<view>
		<uni-list>
			<uni-list-item v-for="(item,index) in list" :key="index" :title="item.title" :to="`/pages/detail/detail?id=${item._id}`"></uni-list-item>
		</uni-list>
		<uni-load-more :status="status" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				reload: false,
				status: 'more',
				tipShow: false,
				pageSize: 20,
				current: 1
			};
		},
		onLoad() {
			// const db = this.$db
			// const dbCmd = db.command
			// db.collection('opendb-news-articles')
			// 	// .where(dbCmd.and([
			// 	// 	{
			// 	// 		publish_date: dbCmd.eq(1600079738467)
			// 	// 	},
			// 	// 	{
			// 	// 		title: dbCmd.eq('signInWithApple')
			// 	// 	},
			// 	// ]))
			// 	.where('publish_date > now - 6132499')
			// 	.get()
			// 	.then(res => {
			// 		console.log(res);
			// 	})
			// 	.catch(err => {
			// 		console.log(err);
			// 	})
			this.getNewsList()
		},
		methods: {
			onPullDownRefresh() {
				this.current = 1
				this.getNewsList(true);
			},
			onReachBottom() {
				this.getNewsList();
			},
			getNewsList(reload) {
				this.status = 'loading'
				const db = this.$db,
					dbCmd = db.command
				db.collection('opendb-news-articles')
					.where({
						publish_date: dbCmd.gt(Date.now() - 365 * 24 * 3600000)
					})
					.orderBy('publish_date', 'desc')
					.skip(this.pageSize * (this.current - 1))
					.limit(this.pageSize)
					.get()
					.then((res) => {
						const tempList = res.result.data
						if (tempList.length === this.pageSize) {
							this.status = 'more'
						} else {
							this.status = 'noMore'
						}
						if (reload) {
							this.list = tempList
							uni.stopPullDownRefresh()
						} else {
							this.list = this.list.concat(tempList)
						}
						if (tempList.length) {
							this.current++
						}
					}).catch((err) => {
						uni.showModal({
							content: '请求失败，请稍后再试：' + err,
							showCancel: false
						})
					})
			}
		}
	};
</script>

<style>
</style>
